<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------

namespace plugins\code_generate\controller;

use cmf\controller\PluginAdminBaseController;
use plugins\code_generate\model\FormModel;
use think\Db;
use tree\Tree;

/**
 * Class AdminIndexController.
 */

class AdminIndexController extends PluginAdminBaseController
{
    protected function initialize()
    {
        parent::initialize();
        $adminId = cmf_get_current_admin_id(); //获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign('admin_id', $adminId);
        }
    }
    
    /**
     * 代码生成器
     * @adminMenu(
     *     'name'   => '代码生成器',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '代码生成器插件',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $tree     = new Tree();
        $result   = Db::name('AdminMenu')->where("status",1)->order(["list_order" => "ASC"])->select();
        $array    = [];
        foreach ($result as $r) {            
            $array[] = $r;
        }
        $str = "<option value='\$id'>\$spacer \$name</option>";
        $tree->init($array);
        $selectCategory = $tree->getTree(0, $str);
        $this->assign("category", $selectCategory);
        return $this->fetch('/admin_index');
    }

    public function settingPost()
    {
        $parm = $this->request->param();
        $model = new FormModel();
        $result         = $this->validate($parm, 'Form');
        if ($result !== true) {
            $this->error($result);
        }
        $controllers = $parm['controllers'];
        $fields =$model->getField($parm['model']);
        foreach ($fields as $key=>$val){
            $field = '{$vo.'.$val["COLUMN_NAME"].'}';
            $fields[$key]['field'] = $field;
        }
        //index
        $indexViews = $this->indexViews($parm,$model,$controllers,$fields);
        //add
        $addViews = $this->addViews($parm,$model,$controllers,$fields);
        //edit
        $editViews = $this->editViews($parm,$model,$controllers,$fields);
        //show
        $showViews = $this->showViews($parm,$model,$controllers,$fields);
        if (            
            $model->viewAdd($parm,$indexViews,$addViews,$editViews,$showViews)== true  &&           
            $model->controllerAdd($parm,$fields)== true  &&
            $model->modelAdd($parm,$fields) == true &&
            $model->validateAdd($parm,$fields)== true
        ){
            $this->success("生成成功");
        }
        $this->success("生成失败");
    }

    protected function indexViews($parm,$model,$controllers,$fields)
    {
        $id = '$vo["id"]';
         //var_dump($fields);exit;
        $this->assign('foreach','<foreach name="data" item="vo">');
        $this->assign('foreachEnd','</foreach>');
        //字段
        $this->assign('fields',$fields);
        //列表
        $this->assign('index',"{:url('$controllers/index')}");//控制器
        //添加
        $this->assign('add',"{:url('$controllers/add')}");//控制器
        //编辑
        $this->assign('edit',"{:url('$controllers/edit',array('id'=>$id))}");//控制器
        //查看
        $this->assign('show',"{:url('$controllers/show',array('id'=>$id))}");//控制器
        //删除
        $this->assign('delete',"{:url('$controllers/delete',array('id'=>$id))}");//控制器
        //翻页
        $this->assign('page','{$page}');
        $this->assign('head','public@header');
        //变量
        $this->assign('q_name','{$name|default=\'\'}');

        return $this->fetch('/indexViews');
    }

    protected function addViews($parm,$model,$controllers,$fields)
    {
        $this->assign('head','public@header');
        //列表
        $this->assign('index',"{:url('$controllers/index')}");//控制器
        //添加
        $this->assign('add',"{:url('$controllers/add')}");//控制器
        //保存
        $this->assign('addPost',"{:url('$controllers/addPost')}");//控制器
        //字段
        $this->assign('fields',$fields);
        return $this->fetch('/addViews');
    }

    protected function editViews($parm,$model,$controllers,$fields)
    {
        $this->assign('head','public@header');
        $id = '$vo["id"]';
        //列表
        $this->assign('index',"{:url('$controllers/index')}");//控制器
        //编辑
        $this->assign('edit',"{:url('$controllers/edit',array('id'=>$id))}");//控制器
        //保存
        $this->assign('editPost',"{:url('$controllers/editPost')}");//控制器
        //字段
        $this->assign('fields',$fields);
        return $this->fetch('/editViews');
    }
    
    protected function showViews($parm,$model,$controllers,$fields)
    {
        $this->assign('head','public@header');
        //列表
        $this->assign('index',"{:url('$controllers/index')}");//控制器
        //添加
        $this->assign('add',"{:url('$controllers/add')}");//控制器
        //字段
        $this->assign('fields',$fields);
        return $this->fetch('/showViews');
    }
}
